<template>
  <el-uploadController
    ref="uploadController"
    action="#"
    accept=".xlsx"
    type="primary"
    :http-request="uploadOk"
    :on-change="uploadExcel"
    :show-file-list="false"
    :file-list="fileList"
    :limit="100"
    :auto-uploadController="false"
    drag
  >
    <i class="el-icon-uploadController"></i>
    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    <template #tip>
      <div class="el-upload__tip text-center">
        <span>仅允许导入xlsx格式文件。</span>
      </div>
    </template>
  </el-uploadController>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogDao">取 消</el-button>
  </div>
</template>

<script setup>
const fileList = ref([]);
const uploadOk = (param) => {
  console.log(`上传成功---`, param);
  const fileFormData = new FormData();
  fileFormData.append("uploadFile", param.file);
  console.log(`上传参数---`, param.file);
  crudUser.uploadExcel(fileFormData).then((res) => {
    console.log(`上传成功？？？---`, res);
  });
};

const uploadExcel = (file, fileList) => {
  const fileTemp = file.raw;
  const fileName = file.raw.name;
  const fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
  if (fileTemp) {
    if (fileType === "xlsx" || fileType === "xls") {
      const formData = new FormData(); //这一步不能变
      formData.append("file", file.raw);
    } else {
      this.$message({
        type: "warning",
        message: "附件格式错误，请重新上传！",
      });
    }
  } else {
    this.$message({
      type: "warning",
      message: "请上传附件！",
    });
  }
};
</script>
